// DOM元素
const messageAlert = document.getElementById('messageAlert');

// 显示消息提示
function showMessage(message, type = 'info') {
    messageAlert.textContent = message;
    messageAlert.className = `message-alert ${type}`;
    messageAlert.style.display = 'block';
    
    setTimeout(() => {
        messageAlert.style.display = 'none';
    }, 3000);
}

// 注册表单处理
document.getElementById('registerForm').addEventListener('submit', async (e) => {
    e.preventDefault();
    const username = document.getElementById('regUsername').value;
    const password = document.getElementById('regPassword').value;
    const phone = document.getElementById('regPhone').value;
    const email = document.getElementById('regEmail').value;
    
    try {
        const response = await fetch('/api/auth/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, password, phone, email })
        });
        
        const data = await response.json();
        
        if (response.ok) {
            showMessage('注册成功，正在跳转到登录页面...', 'success');
            setTimeout(() => {
                window.location.href = 'index.html';
            }, 1500);
        } else {
            showMessage(data.message, 'error');
        }
    } catch (error) {
        showMessage('注册失败，请稍后重试', 'error');
    }
}); 